package com.azdebugit.map.entry;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 遍历Map的方式
 *
 * @author MONEY
 */
public class TestHashMap {
    public static void main(String[] arg0) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("1", "da");
        map.put("2", "jia");
        map.put("3", "hao");
        //第一种使用 keyset 返回键的set
        Set<String> key = map.keySet();
        for (String k : key) {
            System.out.println(map.get(k));
        }
        //第二种 返回值的Collection
        for (String v : map.values()) {
            System.out.println("value= " + v);
        }
        //第三种，使用Map.Entry
        //map.entrySet()返回map映射的关系视图
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
        }
        //第四种 使用迭代器iterator遍历
        Iterator<Map.Entry<String, String>> itera = map.entrySet().iterator();
        while (itera.hasNext()) {
            Map.Entry<String, String> entry = itera.next();
            System.out.println("key " + entry.getKey() + " value " + entry.getValue());
        }
    }
}